﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using System.Collections;

namespace chuti
{
    public partial class Form1 : Form
    {
        public List<string> TKList;
        public List<string> XZList;
        public List<string> JDList;
        public List<string> tkRes;
        public List<string> xzRes;
        public List<string> jdRes;
        MSWord.Application wordApp;                   //Word应用程序变量 
        MSWord.Document wordDoc;                  //Word文档变量
        public Form1()
        {
            InitializeComponent();
            TKList = new List<string>();
            XZList = new List<string>();
            JDList = new List<string>();
            this.lbXZInfo.Text = "选择题";
            this.lbJDInfo.Text = "简答题";
            this.lbTKInfo.Text = "填空题";
            this.lbXZInfo.Enabled = false;
            this.lbTKInfo.Enabled = false;
            this.lbJDInfo.Enabled = false;
            this.lb_JDRes.Text = "简答";
            this.lb_TKRes.Text = "选择";
            this.lb_XZRes.Text = "填空";
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }
        //选择题库文件
        private void btn_SelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "word文档|*.docx";
            opf.RestoreDirectory = true;
            if(opf.ShowDialog() == DialogResult.OK)
            {
                
                string filepath = opf.FileName;
                this.txt_FilePath.Text = filepath;
                //打开文档
                //创建word应用程序
                wordApp = new MSWord.ApplicationClass(); //初始化
                //设置该应用程序可见
                wordApp.Visible = true;
                Object Nothing = Missing.Value;
                //wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //打开题库
                object file = filepath;
                object unknow = Type.Missing;
                wordDoc = wordApp.Documents.OpenNoRepairDialog(ref file);
                int pcount = wordDoc.Paragraphs.Count;
                
                int nTKStart = 0;
                int nXZStart = 0;
                int nJDStart = 0;
                //找出填空题的开始段落
                for(int i = 1; i <= pcount; i++)
                {
                    MSWord.Paragraph par = wordDoc.Paragraphs[i];
                    string content = par.Range.Text.Trim();
                    if (content == "一、填空题")
                    {
                        nTKStart = i;
                    }else if(content == "二、选择题")
                    {
                        nXZStart = i;
                    }else if(content == "三、简答题")
                    {
                        nJDStart = i;
                    }

                }
                
                int nTKCount = nXZStart - nTKStart;
                int nXZCount = nJDStart-nXZStart;
                int nJDCount = pcount-nJDStart;
                //开始统计填空题的数量
                for(int i = 1; i < nTKCount; i++)
                {
                    MSWord.Paragraph par = wordDoc.Paragraphs[i+nTKStart];
                    string content = par.Range.Text;
                    TKList.Add(content);
                    
                }
                //开始统计选择题的数量
                string xzcontent = "";
                for (int i = 1; i < nXZCount; i++)
                {
                    MSWord.Paragraph par = wordDoc.Paragraphs[i + nXZStart];
                    
                    if (i % 5 != 0)
                    { 
                        xzcontent += par.Range.Text; 
                    }
                    else
                    {
                        xzcontent += par.Range.Text;
                        XZList.Add(xzcontent);
                        xzcontent = "";
                    }
                    
                    
                }
                //开始统计简答题的数量
                for (int i = 1; i < nJDCount; i++)
                {
                    MSWord.Paragraph par = wordDoc.Paragraphs[i + nJDStart];
                    string content = par.Range.Text;
                    JDList.Add(content);
                    
                }
                if (this.TKList.Count > 0) this.lbTKInfo.Enabled = true;
                if (this.XZList.Count > 0) this.lbXZInfo.Enabled = true;
                if (this.JDList.Count > 0) this.lbJDInfo.Enabled = true;
                this.lbTKInfo.Text = string.Format("填空题共{0}道", TKList.Count);
                this.lbXZInfo.Text = string.Format("选择题共{0}道", XZList.Count);
                this.lbJDInfo.Text = string.Format("简答题共{0}道", JDList.Count);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (wordApp != null)
                {
                    if (wordDoc != null)
                    {
                        wordDoc.Close();
                    }
                    wordApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                }
            }
            catch(Exception ee)
            {

            }
            
        }

        private void lbTKInfo_Click(object sender, EventArgs e)
        {
            TiKuInfoForm tikuinfoForm = new TiKuInfoForm("填空题", TKList);
            tikuinfoForm.ShowDialog();
        }

        private void lbXZInfo_Click(object sender, EventArgs e)
        {
            TiKuInfoForm tikuinfoForm = new TiKuInfoForm("选择题", XZList);
            tikuinfoForm.ShowDialog();
        }

        private void lbJDInfo_Click(object sender, EventArgs e)
        {
            TiKuInfoForm tikuinfoForm = new TiKuInfoForm("简答题", JDList);
            tikuinfoForm.ShowDialog();
        }
        //开始抽题
        private void btn_ChouTi_Click(object sender, EventArgs e)
        {
            //检查设置的题目数量是否大于对应类型的题目总数
            int nTk = Convert.ToInt32(num_TK.Value);
            int nXZ = Convert.ToInt32(num_XZ.Value);
            int nJD = Convert.ToInt32(num_PD.Value);

            if(nTk > TKList.Count || nXZ > XZList.Count || nJD > JDList.Count)
            {
                MessageBox.Show("抽题数量不能大于题目总数","提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return;
            }
            //抽题
            tkRes = ChouTi(TKList, nTk,"填空");
            xzRes = ChouTi(XZList, nXZ,"选择");
            jdRes = ChouTi(JDList, nJD,"简答");
            
        }
        private List<string> ChouTi(List<string> ls,int count,string type)
        {
            Hashtable hashtable = new Hashtable();
            List<string> lsRes = new List<string>();
            System.Random a = new Random(System.DateTime.Now.Millisecond);
            while (hashtable.Count != count)
            {
                int randKey = a.Next(ls.Count);
                if (!hashtable.ContainsValue(randKey))
                {
                    hashtable.Add(randKey, randKey);
                    lsRes.Add(ls[randKey]);
                    string tip = string.Format("{0}已抽{1}道",type, lsRes.Count);
                    if(type == "填空")
                    {
                        this.lb_TKRes.Text = tip;
                    }
                    else if(type == "选择")
                    {
                        this.lb_XZRes.Text = tip;
                    }else if(type == "简答")
                    {
                        this.lb_JDRes.Text = tip;
                    }
                }
                
            }        
            return lsRes;
        }
        //导出word
        private void btn_ImportWord_Click(object sender, EventArgs e)
        {
            MSWord.Application wordApp1;                   //Word应用程序变量 
            MSWord.Document wordDoc1;                  //Word文档变量
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "word文档|*.docx";
            sfd.RestoreDirectory = true;
            if(sfd.ShowDialog() == DialogResult.OK)
            {
                string filePath = sfd.FileName;
                wordApp1 = new MSWord.ApplicationClass(); //初始化

                wordApp.Visible = true;//使文档可见
               
                //如果已存在，则删除
               if (File.Exists((string)filePath))
                {
                    File.Delete((string)filePath);
                }
                Object Nothing = Missing.Value;
                wordDoc1 = wordApp1.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //写入填空题
                string strContent = "一、填空题\n";
                wordDoc1.Paragraphs.Last.Range.Font.Name = "黑体";
                wordDoc1.Paragraphs.Last.Range.Text = strContent;

                foreach(string  timu in tkRes)
                {
                    //
                    int nDot = timu.IndexOf(".");
                    string timustr = (tkRes.IndexOf(timu) + 1).ToString() + timu.Substring(nDot);
                    wordDoc1.Paragraphs.Last.Range.Text = timustr;
                }
                //写入选择题
                string strContent2 = "二、选择题\n";
                wordDoc1.Paragraphs.Last.Range.Font.Name = "黑体";
                wordDoc1.Paragraphs.Last.Range.Text = strContent2;

                foreach (string timu in xzRes)
                {
                    int nDot = timu.IndexOf(".");
                    string timustr = (xzRes.IndexOf(timu) + 1).ToString() + timu.Substring(nDot);
                    wordDoc1.Paragraphs.Last.Range.Text = timustr;
                }
                //写入简答题
                string strContent3 = "三、简答题\n";
                wordDoc1.Paragraphs.Last.Range.Font.Name = "黑体";
                wordDoc1.Paragraphs.Last.Range.Text = strContent3;

                foreach (string timu in jdRes)
                {
                    int nDot = timu.IndexOf(".");
                    string timustr = (jdRes.IndexOf(timu) + 1).ToString() + timu.Substring(nDot);
                    wordDoc1.Paragraphs.Last.Range.Text = timustr;
                }

                //写完关闭文档
                object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;// office 2007就是wdFormatDocumentDefault
                //将wordDoc文档对象的内容保存为DOCX文档
                object file = filePath;
                wordDoc1.SaveAs(ref file, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                 //关闭wordDoc文档对象
                wordDoc1.Close(ref Nothing, ref Nothing, ref Nothing);
                 //关闭wordApp组件对象
                wordApp1.Quit(ref Nothing, ref Nothing, ref Nothing);
                MessageBox.Show("试题导出成功！");
            }
        }
    }
}
